<template>
  <!-- 关注组件 -->
  <van-button class="follow-btn" type="info" color="#3296fa" round size="small" icon="plus" v-if="!is_followed" @click="followUser" :loading="isLoading">关注</van-button>
  <van-button v-else class="follow-btn" round size="small" @click="followUser" :loading="isLoading">已关注</van-button>
</template>

<script>
import { addFollow, deleteFollow } from '@/api/user'
export default {
  name: 'FollowUser',
  model: {
    prop: 'is_followed',
    event: 'update-is-followed'
  },
  props: {
    is_followed: {
      type: Boolean,
      required: true
    },
    aut_id: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
      isLoading: false
    }
  },

  created () {

  },

  methods: {
    // 关注用户
    async followUser () {
      this.isLoading = true// 显示loading效果
      try {
        if (this.is_followed) {
          // 取消关注
          await deleteFollow(this.aut_id)
        } else {
          // 添加关注
          await addFollow(this.aut_id)
        }
        // this.is_followed = !this.is_followed
        this.$emit('update-is-followed', !this.is_followed)
      } catch (err) {
        if (err.response && err.response.status === 400) {
          return this.$toast.fail('不能关注自己')
        }
        this.$toast.fail('关注操作失败')
      }
      this.isLoading = false// 关闭loading效果
    }
  }
}
</script>

<style scoped lang='less'>
</style>
